<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2025/11/18
  Time: 10:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
# 数据库连接信息
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=root

# 连接池配置
jdbc.initialSize=5
jdbc.maxActive=20
jdbc.maxWait=3000
jdbc.minIdle=3
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 扫描Service层注解 -->
    <context:component-scan base-package="com.ssm.service"/>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 开启事务注解驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
package com.ssm.pojo;

import lombok.Data; // 需导入lombok依赖，或手动生成getter/setter

/**
* 用户实体类（对应数据库user表）
*/
@Data
public class User {
private Integer id;         // 主键ID
private String username;    // 用户名（唯一）
private String password;    // 密码
private String email;       // 邮箱
private String phone;       // 手机号（可选）
}
package com.ssm.exception;

/**
* 用户名已存在异常
*/
public class UserExistsException extends RuntimeException {
public UserExistsException() {
super();
}

public UserExistsException(String message) {
super(message);
}
}
package com.ssm.mapper;

import com.ssm.pojo.User;
import org.apache.ibatis.annotations.Param;

/**
* 用户Mapper接口（MyBatis）
*/
public interface UserMapper {
/**
* 根据用户名查询用户（判断用户名是否已存在）
*/
User selectUserByUsername(@Param("username") String username);

/**
* 新增用户（注册）
*/
int insertUser(User user);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace必须与Mapper接口全路径一致 -->
<mapper namespace="com.ssm.mapper.UserMapper">

    <!-- 根据用户名查询用户 -->
    <select id="selectUserByUsername" parameterType="String" resultType="User">
        SELECT id, username, password, email, phone FROM user WHERE username = #{username}
    </select>

    <!-- 新增用户 -->
    <insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO user (username, password, email, phone)
        VALUES (#{username}, #{password}, #{email}, #{phone})
    </insert>
</mapper>
package com.ssm.service;

import com.ssm.pojo.User;

/**
* 用户服务层接口
*/
public interface UserService {
/**
* 用户注册
* @param user 注册用户信息
* @throws Exception 用户名已存在等异常
*/
void register(User user) throws Exception;
}
package com.ssm.service.impl;

import com.ssm.mapper.UserMapper;
import com.ssm.pojo.User;
import com.ssm.service.UserService;
import com.ssm.exception.UserExistsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* 用户服务层实现类
*/
@Service // 标记为Service组件，交给Spring管理
@Transactional // 开启事务
public class UserServiceImpl implements UserService {

@Autowired // 自动注入Mapper接口（MyBatis动态代理实现）
private UserMapper userMapper;

@Override
public void register(User user) throws Exception {
// 1. 校验用户名是否已存在
User existUser = userMapper.selectUserByUsername(user.getUsername());
if (existUser != null) {
// 用户名已存在，抛出自定义异常
throw new UserExistsException("用户名[" + user.getUsername() + "]已被注册！");
}

// 2. 用户名不存在，执行注册（密码可在此处加密，如MD5）
userMapper.insertUser(user);
}
}
</body>
</html>
